#
# Imports
#
import os
import sys
import urllib
import xbmc
import xbmcgui
import xbmcplugin
import xbmczone_constants
from xml.dom import minidom

#
# Main class
#
class Main:
	#
	# Init
	#
	def __init__( self ) :
		# Parse plugin parameters...
		self.params = dict(part.split('=') for part in sys.argv[ 2 ][ 1: ].split( '&' ))
		
		# Get the plugins...
		plugins = self.getPlugins()
	
	#
	# Get plugins...
	#
	def getPlugins( self ) :
		# 
		# Init
		#
		category_search  = urllib.unquote( self.params[ "category" ] )		
		where_to_install = { "Music Plugin"   : os.path.join( xbmczone_constants.XBMC_HOME_PATH, "plugins", "music" ),
							 "Picture Plugin" : os.path.join( xbmczone_constants.XBMC_HOME_PATH, "plugins", "pictures" ),
							 "Program Plugin" : os.path.join( xbmczone_constants.XBMC_HOME_PATH, "plugins", "programs" ),
							 "Video Plugin"   : os.path.join( xbmczone_constants.XBMC_HOME_PATH, "plugins", "video" ) }
		plugin_icon      = { "Music Plugin"   : "DefaultAudio.png",
		                     "Picture Plugin" : "DefaultPicture.png",
							 "Program Plugin" : "DefaultProgram.png",
							 "Video Plugin"   : "DefaultVideo.png" }
		
		# Get XML data...
		usock = urllib.urlopen( "http://www.xbmczone.com/installer/addon_list.asp?type=Plugin&category=%s" %  urllib.quote( category_search ) )
		dom = minidom.parse( usock )
		
		# Parse XML...
		for category_node in dom.getElementsByTagName('Addon') :
			for child in category_node.childNodes:
				# Type
				if child.localName == "Type" :
					type = child.firstChild.data
				# Category
				elif child.localName == "Category" :
					category = child.firstChild.data
				# ID
				elif child.localName == "ID" :
					id = child.firstChild.data
				# Name
				elif child.localName == "Name" :
					name = child.firstChild.data
				# Version
				elif child.localName == "Version" :
					version = child.firstChild.data
				# Author
				elif child.localName == "Author" :
					author = child.firstChild.data
				# Date
				elif child.localName == "Date" :
					date          = child.firstChild.data
					date_elements = date.split("/")
					item_date     = "%s-%s-%s" % ( date_elements[1].zfill(2), date_elements[0].zfill(2), date_elements[2] )
				# Description
				elif child.localName == "Description" :
					description = child.firstChild.data
				# Instructions
				elif child.localName == "Instructions" :
					if child.firstChild != None :
						instructions = child.firstChild.data
					else :
						instructions = ""
						
			# Add entry...
			label  = name + " " + version
			url    = '%s?action=plugin-info&type=%s&id=%s&name=%s&version=%s&author=%s&date=%s&description=%s&instructions=%s&install-path=%s' % \
				   ( sys.argv[ 0 ], type, id, urllib.quote( name ), version, urllib.quote( author ), urllib.quote( date ), urllib.quote( description ), urllib.quote( instructions ), urllib.quote( where_to_install[ category ] ) )
			listitem = xbmcgui.ListItem( label, iconImage = plugin_icon[ category ] )
			listitem.setInfo( type="Video", infoLabels={ "Date" : item_date } )
			xbmcplugin.addDirectoryItem( handle = int(sys.argv[ 1 ]), url = url, listitem=listitem, isFolder=False)			

		# Label (top-right) = "Plugins"
		xbmcplugin.setPluginCategory( handle=int( sys.argv[ 1 ] ), category = "%s - %s" % ( xbmc.getLocalizedString(30001), category_search.replace("Plugin", "" ) ) )

		# Allow sort by Date (to display dates)...
		xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_DATE )
		
		# End of directory...
		xbmcplugin.endOfDirectory( handle=int( sys.argv[ 1 ] ), succeeded=True )

#
# EOF
#